package cz.muni.fi.pa165.sportclubmanager.service;

import cz.muni.fi.pa165.sportclubmanager.dao.UsersDAO;
import cz.muni.fi.pa165.sportclubmanager.dto.UserDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.UserDTOMapping;
import cz.muni.fi.pa165.sportclubmanager.entity.Users;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author juro
 */
@Service
public class UsersServiceImpl implements UsersService {
    
    @Autowired
    private UsersDAO usersDAO;

    @Override
    @Secured({"ROLE_ADMIN"})
    @Transactional
    public void createUser(UserDTO user) {
        usersDAO.createUser(UserDTOMapping.toEntity(user));
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    @Transactional
    public void updateUser(UserDTO user) {
        usersDAO.updateUser(UserDTOMapping.toEntity(user));
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    @Transactional
    public void removeUser(String name) {
        Users user = new Users();
        user.setName(name);
        usersDAO.removeUser(user);
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    @Transactional
    public List<UserDTO> getUsers() {
        List<Users> users = usersDAO.getUsers();
        List<UserDTO> userDTOs = new ArrayList<>();
        for(Users user: users){
            userDTOs.add(UserDTOMapping.toDTO(user));
        }
        return userDTOs;
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    @Transactional
    public UserDTO find(String name) {
        return UserDTOMapping.toDTO(usersDAO.find(name));
    }

    
    
    
}
